/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Search.java
 *
 * Created on Dec 20, 2008, 4:54:52 PM
 */

package GUI;

import Dictionary.Dictionary;
import functions.Breeds;
import functions.Diagnoses;
import functions.Dogy;
import functions.Drugs;
import functions.Items;
import functions.Operations;
import java.awt.Color;
import java.awt.Dimension;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.swing.table.DefaultTableModel;
import model.Breed;
import model.Diagnose;
import model.Drug;
import model.Item;
import model.Operation;

/**
 *
 * @author GiK
 */
public class Catalog extends javax.swing.JPanel {
    // @Exception handler
    private MainFrame mainFrame;
    private String searchText;
    /** Creates new form Search */
    public Catalog(MainFrame mainFr) {
        initComponents();
        mainFrame = mainFr;
        searchText = Dictionary.FIND;
        searchDrugField.setText(searchText);
        searchDrugField.setForeground(Color.gray);
        searchOperField.setText(searchText);
        searchOperField.setForeground(Color.gray);
        searchDiagField.setText(searchText);
        searchDiagField.setForeground(Color.gray);
        searchBreedField.setText(searchText);
        searchBreedField.setForeground(Color.gray);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        addNewButton = new javax.swing.JButton();
        deleteButton = new javax.swing.JButton();
        catalogPane = new javax.swing.JTabbedPane();
        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        drugTable = new javax.swing.JTable();
        searchDrugField = new javax.swing.JTextField();
        jPanel3 = new javax.swing.JPanel();
        jScrollPane3 = new javax.swing.JScrollPane();
        diagTable = new javax.swing.JTable();
        searchDiagField = new javax.swing.JTextField();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        operTable = new javax.swing.JTable();
        searchOperField = new javax.swing.JTextField();
        jPanel6 = new javax.swing.JPanel();
        jScrollPane6 = new javax.swing.JScrollPane();
        breedTable = new javax.swing.JTable();
        searchBreedField = new javax.swing.JTextField();
        jPanel4 = new javax.swing.JPanel();
        jScrollPane4 = new javax.swing.JScrollPane();
        skladName = new javax.swing.JTable();
        jPanel5 = new javax.swing.JPanel();
        jScrollPane5 = new javax.swing.JScrollPane();
        distrTable = new javax.swing.JTable();

        setMaximumSize(new java.awt.Dimension(704, 418));
        setMinimumSize(new java.awt.Dimension(704, 418));
        setPreferredSize(new java.awt.Dimension(704, 418));
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentResized(java.awt.event.ComponentEvent evt) {
                formComponentResized(evt);
            }
        });
        addHierarchyBoundsListener(new java.awt.event.HierarchyBoundsListener() {
            public void ancestorMoved(java.awt.event.HierarchyEvent evt) {
            }
            public void ancestorResized(java.awt.event.HierarchyEvent evt) {
                formAncestorResized(evt);
            }
        });
        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        addNewButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        addNewButton.setText(Dictionary.NEW);
        addNewButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addNewButtonActionPerformed(evt);
            }
        });
        add(addNewButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 350, 93, 40));

        deleteButton.setFont(new java.awt.Font("Tahoma", 1, 14));
        deleteButton.setText(Dictionary.DELETE);
        deleteButton.setMargin(new java.awt.Insets(2, 10, 2, 10));
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });
        add(deleteButton, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 395, 93, 40));

        catalogPane.setTabPlacement(javax.swing.JTabbedPane.LEFT);
        catalogPane.setFont(new java.awt.Font("Tahoma", 0, 14));
        catalogPane.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                catalogPaneStateChanged(evt);
            }
        });

        drugTable.setFont(new java.awt.Font("Tahoma", 0, 14));
        drugTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                Dictionary.TYPE, Dictionary.NAMING, Dictionary.DESCR,
                Dictionary.MEASURE_IN
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
            public void setValueAt(Object value, int row, int column)
            {
                try
                {
                    Item it = (Item)drugTable.getValueAt(row, 1);
                    if(column == 1)
                    {
                        if(it == null)
                        {
                            it = new Item();
                            if(value == null || value.toString().isEmpty())
                            throw new Exception("Нет имени");

                            it.setName((String)value);
                            int id = Items.addToCatalog(it);
                            it.setId(id);
                        }else
                        it.setName((String)value);
                        super.setValueAt(it,row, column);
                    }else
                    super.setValueAt(value, row, column);
                    String type = (String)drugTable.getValueAt(row, 0),
                    name = drugTable.getValueAt(row, 1).toString(),
                    descr = (String)drugTable.getValueAt(row, 2),
                    izmType = (String)drugTable.getValueAt(row, 3);

                    model.Catalog.changeItem(
                        it.getId(),
                        type,
                        name,
                        descr,
                        izmType
                    );
                    drugTable.changeSelection(row, 1, false, false);
                }catch(SQLException e)
                {
                    mainFrame.SQLException(e);
                }catch(Exception ex)
                {
                    mainFrame.FormException(drugTable.getParent(),
                        ex.getMessage());
                }
            }

        });
        drugTable.setRowHeight(23);
        drugTable.addVetoableChangeListener(new java.beans.VetoableChangeListener() {
            public void vetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {
                drugTableVetoableChange(evt);
            }
        });
        jScrollPane1.setViewportView(drugTable);

        searchDrugField.setFont(new java.awt.Font("Tahoma", 0, 14));
        searchDrugField.setText("Найти");
        searchDrugField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                searchDrugFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                searchDrugFieldFocusLost(evt);
            }
        });
        searchDrugField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                searchDrugFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                    .addComponent(searchDrugField, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchDrugField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 439, Short.MAX_VALUE)
                .addContainerGap())
        );

        catalogPane.addTab(Dictionary.DRUGS, jPanel1);

        diagTable.setFont(new java.awt.Font("Tahoma", 0, 14));
        diagTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                Dictionary.RUSSIAN_NAME, Dictionary.LATIN_NAME
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.Object.class
            };
            boolean[] canEdit = new boolean [] {
                true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }

            public void setValueAt(Object value, int row, int column)
            {
                try
                {
                    Diagnose diag = (Diagnose)diagTable.getValueAt(row, 1);
                    if(diag == null)
                    {
                        diag = new Diagnose();
                        if(column == 1)
                        diag.setName((String)value);
                        if(column == 0)
                        diag.setRusName((String)value);
                        int id = Diagnoses.addToCatalog(diag);
                        diag.setId(id);
                        super.setValueAt(diag,row, 1);
                    }else
                    if(column == 1)
                    {
                        diag.setName((String)value);
                        super.setValueAt(diag,row, 1);
                    }else
                    super.setValueAt(value,row, column);
                    String name = diag.getName(),
                    rusName = (String)diagTable.getValueAt(row, 0);

                    model.Catalog.changeDiagnose(diag.getId(),name , rusName);
                }catch(SQLException e)
                {
                    mainFrame.SQLException(e);
                }
            }
        });
        diagTable.setRowHeight(23);
        jScrollPane3.setViewportView(diagTable);

        searchDiagField.setFont(new java.awt.Font("Tahoma", 0, 14));
        searchDiagField.setText("Найти");
        searchDiagField.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                searchDiagFieldFocusGained(evt);
            }
            public void focusLost(java.awt.event.FocusEvent evt) {
                searchDiagFieldFocusLost(evt);
            }
        });
        searchDiagField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                searchDiagFieldKeyReleased(evt);
            }
        });

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchDiagField, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap())
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel3Layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                    .addContainerGap()))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(searchDiagField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(461, Short.MAX_VALUE))
            .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                    .addGap(41, 41, 41)
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.DEFAULT_SIZE, 443, Short.MAX_VALUE)
                    .addContainerGap()))
        );

        catalogPane.addTab(Dictionary.DIAGNOSES, jPanel3);

        operTable.setFont(new java.awt.Font("Tahoma", 0, 14));
        operTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                Dictionary.NAMING
            }

        ) {
            public void setValueAt(Object value, int row, int column)
            {
                Operation oper =
                (Operation)operTable.getValueAt(row, column);
                try
                {
                    if(oper == null)
                    {
                        oper = new Operation();
                        oper.setName((String)value);
                        int id = Operations.addToCatalog(oper);
                        oper.setId(id);
                        super.setValueAt(oper, row, column);
                    }else
                    if(column == 0)
                    {
                        oper.setName((String)value);
                        super.setValueAt(oper, row, column);
                    }
                    String name = oper.getName();

                    model.Catalog.changeOperation(oper.getId(),name);
                }catch(SQLException e)
                {
                    mainFrame.SQLException(e);

                }
            }

        }

    );
    operTable.setRowHeight(23);
    jScrollPane2.setViewportView(operTable);

    searchOperField.setFont(new java.awt.Font("Tahoma", 0, 14));
    searchOperField.setText("Найти");
    searchOperField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            searchOperFieldFocusGained(evt);
        }
        public void focusLost(java.awt.event.FocusEvent evt) {
            searchOperFieldFocusLost(evt);
        }
    });
    searchOperField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            searchOperFieldKeyReleased(evt);
        }
    });

    javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
    jPanel2.setLayout(jPanel2Layout);
    jPanel2Layout.setHorizontalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(searchOperField, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
            .addContainerGap())
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap()))
    );
    jPanel2Layout.setVerticalGroup(
        jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel2Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(searchOperField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(461, Short.MAX_VALUE))
        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(42, 42, 42)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 442, Short.MAX_VALUE)
                .addContainerGap()))
    );

    catalogPane.addTab(Dictionary.OPERATIONS, jPanel2);

    breedTable.setFont(new java.awt.Font("Tahoma", 0, 14));
    breedTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            Dictionary.NAMING
        }

    ) {
        public void setValueAt(Object value, int row, int column)
        {
            Breed breed =
            (Breed)breedTable.getValueAt(row, column);
            try
            {
                if(breed == null)
                {
                    breed = new Breed();
                    breed.setName((String)value);
                    int id = Breeds.addToCatalog(breed);
                    breed.setId(id);
                    super.setValueAt(breed, row, 0);
                }else
                if(column == 0)
                {
                    breed.setName((String)value);
                    super.setValueAt(breed, row, column);
                }
                String name = breed.getName();

                model.Catalog.changeBreed(breed.getId(),name);
            }catch(SQLException e)
            {
                mainFrame.SQLException(e);

            }
        }

    }

    );
    breedTable.setRowHeight(23);
    jScrollPane6.setViewportView(breedTable);

    searchBreedField.setFont(new java.awt.Font("Tahoma", 0, 14));
    searchBreedField.setText(Dictionary.FIND);
    searchBreedField.addFocusListener(new java.awt.event.FocusAdapter() {
        public void focusGained(java.awt.event.FocusEvent evt) {
            searchBreedFieldFocusGained(evt);
        }
        public void focusLost(java.awt.event.FocusEvent evt) {
            searchBreedFieldFocusLost(evt);
        }
    });
    searchBreedField.addKeyListener(new java.awt.event.KeyAdapter() {
        public void keyReleased(java.awt.event.KeyEvent evt) {
            searchBreedFieldKeyReleased(evt);
        }
    });

    javax.swing.GroupLayout jPanel6Layout = new javax.swing.GroupLayout(jPanel6);
    jPanel6.setLayout(jPanel6Layout);
    jPanel6Layout.setHorizontalGroup(
        jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel6Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(searchBreedField, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
            .addContainerGap())
        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel6Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane6, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap()))
    );
    jPanel6Layout.setVerticalGroup(
        jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGroup(jPanel6Layout.createSequentialGroup()
            .addContainerGap()
            .addComponent(searchBreedField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
            .addContainerGap(461, Short.MAX_VALUE))
        .addGroup(jPanel6Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel6Layout.createSequentialGroup()
                .addContainerGap(43, Short.MAX_VALUE)
                .addComponent(jScrollPane6, javax.swing.GroupLayout.PREFERRED_SIZE, 441, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap()))
    );

    catalogPane.addTab(Dictionary.BREEDS, jPanel6);

    skladName.setFont(new java.awt.Font("Tahoma", 0, 14));
    skladName.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            Dictionary.NAMING, Dictionary.DESCR
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Object.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    skladName.setRowHeight(23);
    jScrollPane4.setViewportView(skladName);
    skladName.getColumnModel().getColumn(0).setPreferredWidth(200);
    skladName.getColumnModel().getColumn(0).setMaxWidth(200);

    javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
    jPanel4.setLayout(jPanel4Layout);
    jPanel4Layout.setHorizontalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 636, Short.MAX_VALUE)
        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap()))
    );
    jPanel4Layout.setVerticalGroup(
        jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 495, Short.MAX_VALUE)
        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane4, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                .addContainerGap()))
    );

    catalogPane.addTab(Dictionary.WAREHOUSES, jPanel4);

    distrTable.setFont(new java.awt.Font("Tahoma", 0, 14));
    distrTable.setModel(new javax.swing.table.DefaultTableModel(
        new Object [][] {

        },
        new String [] {
            Dictionary.NAMING, Dictionary.DESCR
        }
    ) {
        Class[] types = new Class [] {
            java.lang.Object.class, java.lang.String.class
        };
        boolean[] canEdit = new boolean [] {
            false, false
        };

        public Class getColumnClass(int columnIndex) {
            return types [columnIndex];
        }

        public boolean isCellEditable(int rowIndex, int columnIndex) {
            return canEdit [columnIndex];
        }
    });
    distrTable.setRowHeight(23);
    jScrollPane5.setViewportView(distrTable);
    distrTable.getColumnModel().getColumn(0).setPreferredWidth(200);
    distrTable.getColumnModel().getColumn(0).setMaxWidth(200);

    javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
    jPanel5.setLayout(jPanel5Layout);
    jPanel5Layout.setHorizontalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 636, Short.MAX_VALUE)
        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 616, Short.MAX_VALUE)
                .addContainerGap()))
    );
    jPanel5Layout.setVerticalGroup(
        jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
        .addGap(0, 495, Short.MAX_VALUE)
        .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 473, Short.MAX_VALUE)
                .addContainerGap()))
    );

    catalogPane.addTab(Dictionary.DISTRIBUTORS, jPanel5);

    add(catalogPane, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 10, -1, 500));
    }// </editor-fold>//GEN-END:initComponents

    private void catalogPaneStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_catalogPaneStateChanged
        int tab = catalogPane.getSelectedIndex();
        try
        {
            switch(tab)
            {
                case 0:
                    fillDrugTable("");
                    break;
                case 1:
                    fillDiagTable("");
                    break;
                case 2:
                    fillOperTable("");
                    break;
                case 3:
                    fillBreedTable("");
                    break;
                case 4:
                    break;
            }
        }
        catch(SQLException e)
        {
            mainFrame.SQLException(e);
        }
    }//GEN-LAST:event_catalogPaneStateChanged

    private void drugTableVetoableChange(java.beans.PropertyChangeEvent evt)throws java.beans.PropertyVetoException {//GEN-FIRST:event_drugTableVetoableChange
        
    }//GEN-LAST:event_drugTableVetoableChange

    private void addNewButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addNewButtonActionPerformed
        int tab = catalogPane.getSelectedIndex();
        DefaultTableModel model;
        try
        {
            switch(tab)
            {
                case 0:
                    model =
                            (DefaultTableModel) drugTable.getModel();
                    model.addRow(new Object[]{"",null,"","",""});
                    drugTable.getSelectionModel().setSelectionInterval(model.getRowCount()-1,model.getRowCount()-1);
                    drugTable.requestFocus();
                    drugTable.scrollRectToVisible(drugTable.getCellRect(model.getRowCount()-1, 1, true));
                    drugTable.editCellAt(model.getRowCount()-1, 1);
                    drugTable.requestFocus();
                    break;
                case 1:
                    model =
                            (DefaultTableModel) diagTable.getModel();
                    model.addRow(new Object[]{"",null});
                    diagTable.getSelectionModel().setSelectionInterval(model.getRowCount()-1,model.getRowCount()-1);
                    diagTable.requestFocus();
                    diagTable.scrollRectToVisible(diagTable.getCellRect(model.getRowCount()-1, 1, true));
                    diagTable.editCellAt(model.getRowCount()-1, 1);
                    diagTable.requestFocus();
                    
                    break;
                case 2:
                    model =
                            (DefaultTableModel) operTable.getModel();
                    model.addRow(new Object[]{null});
                    operTable.getSelectionModel().setSelectionInterval(model.getRowCount()-1,model.getRowCount()-1);
                    operTable.requestFocus();
                    operTable.scrollRectToVisible(operTable.getCellRect(model.getRowCount()-1, 1, true));
                    operTable.editCellAt(model.getRowCount()-1, 0);
                    operTable.requestFocus();
                    
                    break;
                case 3:
                    model =
                            (DefaultTableModel) breedTable.getModel();
                    model.addRow(new Object[]{null});
                    breedTable.getSelectionModel().setSelectionInterval(model.getRowCount()-1,model.getRowCount()-1);
                    breedTable.requestFocus();
                    breedTable.scrollRectToVisible(breedTable.getCellRect(model.getRowCount()-1, 1, true));
                    breedTable.editCellAt(model.getRowCount()-1, 0);
                    breedTable.requestFocus();
                    break;
                case 4:
                    break;
            }
        }
        catch(Exception e)
        {
            mainFrame.Exception(e);
        }
}//GEN-LAST:event_addNewButtonActionPerformed

    private void formComponentResized(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentResized
        //catalogPane.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
        //catalogPane.setMinimumSize(new Dimension(this.getWidth(), this.getHeight()));
        //catalogPane.setBounds(catalogPane.getBounds().x, catalogPane.getBounds().y,
        //                      this.getWidth(), this.getHeight());
    }//GEN-LAST:event_formComponentResized

    private void formAncestorResized(java.awt.event.HierarchyEvent evt) {//GEN-FIRST:event_formAncestorResized
        catalogPane.setPreferredSize(new Dimension(this.getWidth(), this.getHeight()));
        catalogPane.setMinimumSize(new Dimension(this.getWidth(), this.getHeight()));
        catalogPane.setMaximumSize(new Dimension(this.getWidth(), this.getHeight()));
        catalogPane.setSize(new Dimension(this.getWidth(), this.getHeight()));
        catalogPane.setBounds(catalogPane.getBounds().x, catalogPane.getBounds().y,
                              this.getWidth(), this.getHeight());
    }//GEN-LAST:event_formAncestorResized

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int tab = catalogPane.getSelectedIndex();
        DefaultTableModel tModel;
        try
        {
            switch(tab)
            {
                case 0:
                    tModel =
                            (DefaultTableModel) drugTable.getModel();
                    for(int i=drugTable.getSelectedRows().length-1; i >= 0 ; i--)
                    {
                      if(drugTable.getValueAt(drugTable.getSelectedRows()[i], 1) != null )
                      model.Catalog.deleteItem(((Item)drugTable.getValueAt(drugTable.getSelectedRows()[i], 1)).getId());
                      tModel.removeRow(drugTable.getSelectedRows()[i]);
                    }
                    drugTable.setModel(tModel);
                    break;
                case 1:
                    tModel =
                            (DefaultTableModel) diagTable.getModel();
                    for(int i=diagTable.getSelectedRows().length-1; i >= 0 ; i--)
                    {
                      if(diagTable.getValueAt(diagTable.getSelectedRows()[i], 1) != null)
                      model.Catalog.deleteDiagnose(((Diagnose)diagTable.getValueAt(diagTable.getSelectedRows()[i], 1)).getId());
                      tModel.removeRow(diagTable.getSelectedRows()[i]);
                    }
                    diagTable.setModel(tModel);

                    break;
                case 2:
                    tModel =
                            (DefaultTableModel) operTable.getModel();
                    for(int i=operTable.getSelectedRows().length-1; i >= 0 ; i--)
                    {
                      if(operTable.getValueAt(operTable.getSelectedRows()[i], 0) != null)
                      model.Catalog.deleteOperation(((Operation)operTable.getValueAt(operTable.getSelectedRows()[i], 0)).getId());
                      tModel.removeRow(operTable.getSelectedRows()[i]);
                    }
                    operTable.setModel(tModel);

                    break;
                case 3:
                    tModel =
                            (DefaultTableModel) breedTable.getModel();
                    for(int i=breedTable.getSelectedRows().length-1; i >= 0 ; i--)
                    {
                      if(breedTable.getValueAt(breedTable.getSelectedRows()[i], 0) != null)
                      model.Catalog.deleteBreed(((Breed)breedTable.getValueAt(breedTable.getSelectedRows()[i], 0)).getId());
                      tModel.removeRow(breedTable.getSelectedRows()[i]);
                    }
                    breedTable.setModel(tModel);
                    break;
                case 4:
                    break;
            }
        }
        catch(Exception e)
        {
            mainFrame.Exception(e);
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    private void searchDrugFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchDrugFieldFocusGained
        Dogy.focusGained(searchDrugField, searchText);
}//GEN-LAST:event_searchDrugFieldFocusGained

    private void searchDrugFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchDrugFieldFocusLost
        Dogy.focusLost(searchDrugField, searchText);
}//GEN-LAST:event_searchDrugFieldFocusLost

    private void searchDrugFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchDrugFieldKeyReleased
        search();
}//GEN-LAST:event_searchDrugFieldKeyReleased

    private void searchDiagFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchDiagFieldKeyReleased
        search();
}//GEN-LAST:event_searchDiagFieldKeyReleased

    private void searchDiagFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchDiagFieldFocusLost
        Dogy.focusLost(searchDiagField, searchText);
}//GEN-LAST:event_searchDiagFieldFocusLost

    private void searchDiagFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchDiagFieldFocusGained
        Dogy.focusGained(searchDiagField, searchText);
}//GEN-LAST:event_searchDiagFieldFocusGained

    private void searchOperFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchOperFieldFocusGained
        Dogy.focusGained(searchOperField, searchText);
}//GEN-LAST:event_searchOperFieldFocusGained

    private void searchOperFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchOperFieldFocusLost
        Dogy.focusLost(searchOperField, searchText);
}//GEN-LAST:event_searchOperFieldFocusLost

    private void searchOperFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchOperFieldKeyReleased
        search();
}//GEN-LAST:event_searchOperFieldKeyReleased

    private void searchBreedFieldFocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchBreedFieldFocusGained
        Dogy.focusGained(searchBreedField, searchText);
}//GEN-LAST:event_searchBreedFieldFocusGained

    private void searchBreedFieldFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_searchBreedFieldFocusLost
        Dogy.focusLost(searchBreedField, searchText);
}//GEN-LAST:event_searchBreedFieldFocusLost

    private void searchBreedFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_searchBreedFieldKeyReleased
        search();
}//GEN-LAST:event_searchBreedFieldKeyReleased

    public void fillDrugTable(String text) throws SQLException
    {
        DefaultTableModel model = (DefaultTableModel)drugTable.getModel();
        model.setRowCount(0);
        ArrayList<Drug> drugs = Drugs.getByName(text);
        String type="";
        for(int i=0; i< drugs.size(); i++)
        {
            type = drugs.get(i).getType();
            drugs.get(i).setType(null);
            model.addRow(new Object[]{type,
                                      drugs.get(i),
                                      drugs.get(i).getDescr(),
                                      drugs.get(i).getIzmType()});
        }
        drugTable.setModel(model);
    }
    public void fillDiagTable(String text) throws SQLException
    {
        DefaultTableModel model = (DefaultTableModel)diagTable.getModel();
        model.setRowCount(0);
        ArrayList<Diagnose> diags = Diagnoses.getByName(text);
        for(int i=0; i< diags.size(); i++)
        {
            model.addRow(new Object[]{ diags.get(i).getRusName(), diags.get(i)
            });
        }
        diagTable.setModel(model);
    }
    public void fillOperTable(String text) throws SQLException
    {
        DefaultTableModel model = (DefaultTableModel)operTable.getModel();
        model.setRowCount(0);
        ArrayList<Operation> opers = Operations.getByName(text);
        for(int i=0; i< opers.size(); i++)
        {
            model.addRow(new Object[]{ opers.get(i)   });
        }
        operTable.setModel(model);
    }
    public void fillBreedTable(String text) throws SQLException
    {
        DefaultTableModel model = (DefaultTableModel)breedTable.getModel();
        model.setRowCount(0);
        ArrayList<Breed> breeds = Breeds.getByName(text);
        for(int i=0; i< breeds.size(); i++)
        {
            model.addRow(new Object[]{ breeds.get(i)   });
        }
        breedTable.setModel(model);
    }

    private void search()
    {
        int tab = catalogPane.getSelectedIndex();
        try
        {
            switch(tab)
            {
                case 0:

                    fillDrugTable(searchDrugField.getText());
                    break;
                case 1:
                    fillDiagTable(searchDiagField.getText());
                    break;
                case 2:
                    fillOperTable(searchOperField.getText());
                    break;
                case 3:
                    fillBreedTable(searchBreedField.getText());
                    break;
                case 4:
                    break;
            }
        }
        catch(Exception e)
        {
            mainFrame.Exception(e);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton addNewButton;
    private javax.swing.JTable breedTable;
    private javax.swing.JTabbedPane catalogPane;
    private javax.swing.JButton deleteButton;
    private javax.swing.JTable diagTable;
    private javax.swing.JTable distrTable;
    private javax.swing.JTable drugTable;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JPanel jPanel6;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JScrollPane jScrollPane6;
    private javax.swing.JTable operTable;
    private javax.swing.JTextField searchBreedField;
    private javax.swing.JTextField searchDiagField;
    private javax.swing.JTextField searchDrugField;
    private javax.swing.JTextField searchOperField;
    private javax.swing.JTable skladName;
    // End of variables declaration//GEN-END:variables

}
